'''
Created on Mar 26, 2013

@author: Petko
'''
import unittest
from utils.sententialyzer_client import SententialyzerClient
import json
from urllib2 import HTTPError


class CreateViewTest(unittest.TestCase):

    sc = SententialyzerClient()
    
    def setUp(self):
        self.sc.commenterId = 1


    def tearDown(self):
        #result = self.sc.viewListOp('all_base_opinions', 'newest', access_token = self.sc.accessToken)
        #page = result['page']
        
        pass

    def _testDeleteView(self):
        self.sc.authorize('moderator1', 'test', 'view_adm')
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.postDeleteView("Bulgaria_not used")
        self.assertEqual(cm.exception.code, 404)
        
    def _testCreateView(self):
        self.sc.authorize('moderator1', 'test', 'view_adm')
        
        try:
            result = self.sc.postDeleteView("Bulgaria")
            print json.dumps(result)
        except HTTPError:
            pass
        result = self.sc.postCreateView("Bulgaria", "moderators:m sent_default_group:l")
        print json.dumps(result)
        
        with self.assertRaises(HTTPError) as cm:
            result = self.sc.postCreateView("Bulgaria", "moderators:m sent_default_group:l")
        self.assertEqual(cm.exception.code, 409)
        
    def _testAddOpinions(self):
        self.sc.authorize('moderator1', 'test', 'view_adm bopin_c')
        
        try:
            self.sc.postDeleteView("Bulgaria")
        except HTTPError:
            pass
        
        self.sc.postCreateView("Bulgaria", "moderators:m sent_default_group:l")
        op1 = self.sc.createBaseOp("testAddOpinions 1", "bla bla bla", "moderators:d sent_default_group:rc")
        op2 = self.sc.createBaseOp("testAddOpinions 2", "bla bla bla", "moderators:d sent_default_group:rc")
        op3 = self.sc.createBaseOp("testAddOpinions 3", "bla bla bla", "moderators:d sent_default_group:rc")
        self.sc.createBaseOp("testAddOpinions 4", "bla bla bla", "moderators:d sent_default_group:rc")
        
        opIds = set([op1['id'], op2['id'], op3['id']])
        result = self.sc.addOpsToView("Bulgaria", opIds)
        result = set(result)
        
        self.assertEqual(opIds, result)
        
        self.sc.authAndCreateOp(op1['id'], op1['id'], 'positive')
        
        self.sc.authAndCreateOp(op2['id'], op2['id'], 'positive')
        self.sc.authAndCreateOp(op2['id'], op2['id'], 'positive')
        
        result = self.sc.viewListOp("Bulgaria", 'newest')
        
        self.assertEqual(result['totalResults'], 3)
        
        result = self.sc.viewListOp("Bulgaria", 'most_positive', limit = 2)
        
        opList = result['page']
        
        self.assertEqual(opList[0]['id'], op2['id'])
        self.assertEqual(opList[1]['id'], op1['id'])
    
    def testViewPermissions(self):
        
        self.sc.authorize('moderator1', 'test', 'view_adm bopin_c')
        
        try:
            self.sc.postDeleteView("Bulgaria")
        except HTTPError:
            pass
        
        self.sc.postCreateView("Bulgaria", "moderators:m sent_default_group:l")
        
        op1 = self.sc.createBaseOp("testAddOpinions 1", "bla bla bla", "moderators:d sent_default_group:rc")
        op2 = self.sc.createBaseOp("testAddOpinions 2", "bla bla bla", "moderators:d sent_default_group:rc")
        op3 = self.sc.createBaseOp("testAddOpinions 3", "bla bla bla", "moderators:d sent_default_group:rc")
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.authorize('commenter1', 'test', 'Bulgaria:m')
        self.assertEqual(cm.exception.code, 403)
        
        self.sc.authorize('commenter1', 'test', 'all:lm')
        
        opIds = set([op1['id'], op2['id'], op3['id']])
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.addOpsToView("Bulgaria", opIds)
        self.assertEqual(cm.exception.code, 401)
        
        self.sc.authorize('moderator1', 'test', 'Bulgaria:m')
        
        
        result = self.sc.addOpsToView("Bulgaria", opIds)
        result = set(result)
        
        self.assertEqual(opIds, result)
        
        self.sc.authorize('commenter1', 'test', 'all:lm')
        
        result = self.sc.viewListOp("Bulgaria", order='most_positive', access_token=self.sc.accessToken)
        
        self.assertEqual(result['totalResults'], 3)
    
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()